<template>
    <div>{{ viewStr }}<span :class="{ typer: start && viewStr.length < finalStr.length }"></span></div>
</template>

<script>
export default {
    props: {
        finalStr: {
            type: String
        },
        pauseIndex: {
            type: Number,
            default: 0
        },
        start: {
            type: Boolean,
            default: true
        }
    },
    data() {
        return {
            viewStr: ''
        };
    },
    mounted() {
        if (this.start) {
            setTimeout(this.typeWriter, 1500);
        }
    },
    watch: {
        start() {
            if (this.start == true) {
                this.viewStr = '';
                setTimeout(this.typeWriter, 1500);
            }
        }
    },
    methods: {
        typeWriter() {
            if (this.viewStr.length < this.finalStr.length) {
                this.viewStr += this.finalStr.charAt(this.viewStr.length);
                if (this.viewStr.length == this.pauseIndex) {
                    setTimeout(this.typeWriter, 1000);
                } else {
                    setTimeout(this.typeWriter, 200);
                }
            }
        }
    }
}
</script>

<style lang="scss">
.typer {
    font-size: 50px;
    font-weight: bold;
    white-space: nowrap;
    overflow: hidden;
    animation: typing 3s steps(30, end), blink 0.5s step-end infinite;
    border-right: 3px solid;
    box-sizing: border-box;
}

@keyframes typing {
    from {
        width: 0;
    }

    to {
        width: 100%;
    }
}

@keyframes blink {
    from,
    to {
        border-color: transparent;
    }

    50% {
        border-color: #2c3e50;
    }
}
</style>